package Threads.ThreadPool.DIY_ThreadPool;

import sun.jvm.hotspot.opto.Block;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class BlockingQueue<T> {

    //task queue
    private Deque<T> queue=new ArrayDeque<T>();

    //lock
    private ReentrantLock lock=new ReentrantLock();

    //conditon for producer
    private Condition fullWaitSet=lock.newCondition();

    private Condition emptyWaitSet=lock.newCondition();

    //capcity of the queue
    private int capcity;

    public BlockingQueue(int capcity){
        this.capcity=capcity;
    }

    //get the task with time limit
    public T poll(long timeout, TimeUnit unit){
        lock.lock();
        try{
            long l = unit.toNanos(timeout);
            while (queue.isEmpty()){
                try {

                    if (l<=0)
                        return null;
                    l=emptyWaitSet.awaitNanos(l);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            T t = queue.removeFirst();
            fullWaitSet.signal();
            return t;
        } finally {
            lock.unlock();
        }
    }

    //get the task without time limit
    public T take(){
        lock.lock();
        try{
            while (queue.isEmpty()){
                try {
                    emptyWaitSet.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            T t = queue.removeFirst();
            fullWaitSet.signal();
            return t;
        }finally {
            lock.unlock();
        }
    }

    //put the task to the queue
    public void put(T task){
        lock.lock();
        try{
            while (queue.size()==capcity){
                try {
                    System.out.println(task+"waiting to enter the taskqueue");
                    fullWaitSet.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            queue.addLast(task);
            System.out.println(task+":enter the taskqueue");
            emptyWaitSet.signal();
        }finally {
            lock.unlock();
        }
    }

    //put the task with time limits
    public boolean offer(T task,long timeout,TimeUnit unit){
        lock.lock();
        try{
            long l = unit.toNanos(timeout);
            while (queue.size()==capcity){
                try {
                    if (l<=0)
                        return false;
                    System.out.println(task+"waiting to enter the taskqueue");
                    l=fullWaitSet.awaitNanos(l);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            queue.addLast(task);
            System.out.println(task+":enter the taskqueue");
            emptyWaitSet.signal();
            return true;
        }finally {
            lock.unlock();
        }
    }

    public int size(){
        lock.lock();
        try{
            return queue.size();
        }finally {
            lock.unlock();
        }
    }


    //put the task to the queue ,if the queue is full, it will cause the rejectPolicy
    public void tryPut(RejectPolicy<T> rejectPolicy,T task){
        lock.lock();
        try {
            if (queue.size()==capcity)
                rejectPolicy.reject(this,task);
            else{
                System.out.println(task+"enter the taskqueue");
                queue.addLast(task);
                emptyWaitSet.signal();
            }

        }finally {
            lock.unlock();
        }
    }


}
